/**
 * 
 */
package tp.pr5;

import tp.pr5.items.Item;
import tp.pr5.items.ItemContainer;

/**
 * @author Carlos Bermúdez y Mario de Benito
 *It represents a place in the city. Places are connected by streets according to the 4 compass directions: North, East, South and West. Every place has a name and a textual description about itself. This description is displayed when the robot arrives at the place.
 A place can represent the spaceship where the robot is safe. When the robot arrives at this place, the robot shuts down and the application will finish.
 */
public class Place implements PlaceInfo{
	private String name;
	private String description;
	private boolean isSpaceShip;
	private ItemContainer items;
	/**
	 * Constructor for the place
	 * @param name
	 * @param isSpaceShip
	 * @param description
	 */
	public Place(String name, boolean isSpaceShip, String description){
		this.name = name;
		this.isSpaceShip = isSpaceShip;
		this.description = description;
		this.items = new ItemContainer();
		
	}
	/**
	 * Returns true if the place is the spaceship
	 * @return
	 */
	public boolean isSpaceship(){
		return 	isSpaceShip;	
	}	
	/**
	 * Overrides toString method
	 */
	public String toString(){
		String text = this.name + Main.LINE_SEPARATOR + this.description + Main.LINE_SEPARATOR; // salto de linea 
		if(this.getInventory().numberOfItems()>0){
			text += "The place contains these objects:"+Main.LINE_SEPARATOR+
			this.getInventory().listItems();
		}else{
			text += "The place is empty. There are no objects to pick";
		}
		return text;
	}
	
	/**
	 * Tries to add an item to the place. 
	 * @param it
	 * @return
	 */
	public boolean addItem(Item it){
		return this.items.addItem(it);
	}
	/**
	 * drops an item in the place inventory if it's not already present
	 * @param it
	 * @return
	 */
	public boolean dropItem(Item it){
		if(!this.items.findItem(it)){
			this.items.addItem(it);			
			return true;
		}else{			
			return false;		
		}
				
	}
	/**
	 * 	Tries to pick an item characterized by a given identifier from the place.
	 * @param id
	 * @return
	 */
	public Item pickItem(String id) {
		return this.items.pickItem(id);
	}
	/**
	 * returns a reference to the place inventory
	 * @return
	 */
	public ItemContainer getInventory(){
		return this.items;
	}
	/**
	 * checks the existence of an item in the place inventory
	 * @param id
	 * @return
	 */
	public boolean existItem(String id) {
		return this.getInventory().findItem(id);
	}
	/**
	 * Returns the name of the place
	 * @return
	 */
	public String getName(){
		return this.name;
	}
	@Override
	public String getDescription() {
		return this.description;
	}
	@Override
	public boolean isSpaceShip() {
		return this.isSpaceShip;
	}
}
